const { findKey } = require('../../../utils/underscore');

// pages/product/address/address.js
const app = getApp(),
    globalData = app.globalData,
    api = require('../../../api/index'),
    utils = require('../../../utils/util');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        isIpx: globalData.isIpx,
        list: [],
        status: 0,
        imageBaseUri: globalData.imageBaseUri,
        active: -1,
        toast: {
            visible: false,
            text: ''
        }
    },

    // 添加新地址
    toNewAddress() {
        wx.navigateTo({
            url: '/pages/product/newAddress/newAddress?isNew=true',
        })
    },

    /**
     * 修改地址
     */
    toModifyAddre(e) {
        wx.navigateTo({
            url: `/pages/product/newAddress/newAddress?item=${JSON.stringify(e.currentTarget.dataset.item)}`
        })
    },

    // // 删除地址
    async deleteAddress(e) {
        console.log('删除地址')
        wx.showLoading({
            title: '删除中',
        })
        try {
            let res = await api.address.delAddress(e.currentTarget.dataset.id);
            console.log(res,'删除成功')
            this.setData({
                toast: {
                    visible: true,
                    text: '删除成功'
                }
            })
            this.getAddressInfo();
        } catch (e) {
            this.setData({
                toast: {
                    visible: true,
                    text: '删除失败'
                }
            })

        } finally {
            wx.hideLoading()
        }
    },

    // 设为默认地址
    async setDefault(e) {
        console.log('设置默认地址')
        let id = e.currentTarget.dataset.id;
        this.setData({
            active: e.currentTarget.dataset.index
        })
        try {
            let res = await api.address.setDefaultAddress({ id }),
                data = res.data.ResData || {};
            console.log(res, '设置默认地址成功')
            this.setData({
                toast: {
                    visible: true,
                    text: '设置成功'
                }
            })
            this.getAddressInfo();
        } catch (e) {
            console.log(e, '设置默认地址失败')
            this.setData({
                toast: {
                    visible: true,
                    text: '设置失败'
                }
            })
        }


    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.reload();
    },

    reload() {
        this.getAddressInfo();
    },

    /**
     * 跨页面更新数据
     * 添加地址
     */
    pageDirty() {
        this.setData({ dirty: true });
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.getAddressInfo();
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    // 获取地址
    async getAddressInfo() {
        try {
            let res = await api.address.getAddressList(),
                data = res.data.ResData || [];
            this.setData({
                list: data,
                dirty: false,
                status: 2
            });
            console.log(res, '地址列表')
        } catch (e) {
            console.log(e, '获取地址失败')
        }
    },
})